package cn.tj212.xiaoyin.opt.controller;

import cn.tj212.xiaoyin.common.util.EncryptUtil;
import cn.tj212.xiaoyin.common.web.JsonView;
import cn.tj212.xiaoyin.common.web.SessionContext;
import cn.tj212.xiaoyin.core.auth.domain.AuthUser;
import cn.tj212.xiaoyin.core.auth.service.IAuthUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户登录&注册
 */
@Controller
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private IAuthUserService authUserService;

    @RequestMapping("/login")
    public ModelAndView login() {
        if (SessionContext.isLogin()) {
            return new ModelAndView("redirect:/index.html");
        }
        return new ModelAndView("auth/login");
    }

    @RequestMapping("/doLogin")
    public ModelAndView doLogin(AuthUser user, String identifyCode, HttpServletRequest request) {
        //如果已经登陆过
        if (SessionContext.getAuthUser() != null) {
            return new ModelAndView("redirect:/index.html");
        }
        //验证码判断
        if (identifyCode != null && identifyCode.equalsIgnoreCase(SessionContext.getIdentifyCode(request))) {
            ModelAndView mv = new ModelAndView("auth/login");
            mv.addObject("errcode", 1);
            return mv;
        }
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), EncryptUtil.encodeByMD5(user.getPassword()));
        try {
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.login(token);
            return new ModelAndView("redirect:/index.html");
        } catch (AuthenticationException e) {
            ModelAndView mv = new ModelAndView("auth/login");
            mv.addObject("errcode", 2);
            return mv;
        }
    }

    @RequestMapping("/register")
    public ModelAndView register(){
        if (SessionContext.isLogin()){
            return new ModelAndView("redirect:/index.html");
        }
        return new ModelAndView("auth/register");
    }

    @RequestMapping("/doRegister")
    @ResponseBody
    public String doRegister(AuthUser authUser,String identifyCode,HttpServletRequest request){
        //验证码判断
        if (identifyCode!=null&&identifyCode.equalsIgnoreCase(SessionContext.getIdentifyCode(request))){
            return JsonView.render(2);
        }
        AuthUser tmpUser=authUserService.getByUsername(authUser.getUsername());
        if (tmpUser!=null){
            return JsonView.render(1);
        }else {
            authUser.setPassword(EncryptUtil.encodeByMD5(authUser.getPassword()));
            authUserService.createSelectivity(authUser);
            return JsonView.render(0);
        }
    }

    @RequestMapping("/logout")
    public ModelAndView logout(HttpServletRequest request){
        SessionContext.shiroLogout();
        return new ModelAndView("redirect:/index.html");
    }
}
